import { createRouter, createWebHashHistory } from 'vue-router'
import Login from '@/views/Login';
import Main from '@/views/Main';
import {ElMessage} from "element-plus";
import {useStore} from "vuex";



const routes = [
  {path: '/', name: 'Login', component: Login},
  {
    path: '/Main', name: 'Main', component: Main,
    redirect: '/DashBoard',
    children: [
      {path: '/DashBoard', name: 'DashBoard', component:()=>import('@/views/DashBoard')},
      {path: '/rms/Room', name: 'Room', component: () => import('@/views/rms/Room')},
      {path: '/rms/RoomInsert', name: 'RoomInsert', component: () => import('@/views/rms/RoomInsert')},
      {path: '/rms/RoomUpdate', name: 'RoomUpdate', component: () => import('@/views/rms/RoomUpdate')},
      {path: '/rms/Assets', name: 'Assets', component: () => import('@/views/rms/Assets')},
      {path: '/rms/AssetsInsert', name: 'AssetsInsert', component: () => import('@/views/rms/AssetsInsert')},
      {path: '/rms/AssetsUpdate', name:' AssetsUpdate', component: () => import('@/views/rms/AssetsUpdate')},
      {path: '/rms/School', name: 'School', component: () => import('@/views/rms/School')},
      {path: '/rms/SchoolInsert', name: 'SchoolInsert', component: () => import('@/views/rms/SchoolInsert')},
      {path: '/rms/SchoolUpdate', name: 'SchoolUpdate', component: () => import('@/views/rms/SchoolUpdate')},
      {path: '/ums/Dept', name: 'Dept', component: () => import('@/views/ums/Dept')},
      {path: '/ums/DeptInsert', name: 'DeptInsert', component: () => import('@/views/ums/DeptInsert')},
      {path: '/ums/DeptUpdate', name: 'DeptUpdate', component: () => import('@/views/ums/DeptUpdate')},
      {path: '/ums/Emp', name: 'Emp', component: () => import('@/views/ums/Emp')},
      {path: '/ums/EmpInsert', name: 'EmpInsert', component: () => import('@/views/ums/EmpInsert')},
      {path: '/ums/EmpUpdate', name: 'EmpUpdate', component: () => import('@/views/ums/EmpUpdate')},
      {path: '/EmpRoles', name: 'EmpRoles',component:()=>import('@/views/EmpRoles')},
      {path: '/ums/Role', name: 'Role', component: () => import('@/views/ums/Role')},
      {path: '/ums/RoleInsert', name: 'RoleInsert', component: () => import('@/views/ums/RoleInsert')},
      {path: '/ums/RoleUpdate', name: 'RoleUpdate', component: () => import('@/views/ums/RoleUpdate')},
      {path: '/RolePermissions', name: 'RolePermissions',component:()=>import('@/views/RolePermissions')},
      {path: '/ums/Permission', name: 'Permission', component: () => import('@/views/ums/Permission')},
      {path: '/ums/PermissionInsert', name: 'PermissionInsert', component: () => import('@/views/ums/PermissionInsert')},
      {path: '/ums/PermissionUpdate', name: 'PermissionUpdate', component: () => import('@/views/ums/PermissionUpdate')},

      {path: '/ums/personal/Personal', name: 'Personal', component: () => import('@/views/ums/personal/Personal')},
      {path: '/ums/personal/PersonalUpdate', name: 'PersonalUpdate', component: () => import('@/views/ums/personal/PersonalUpdate')},





    ]
  },

]
const router = createRouter({
  history: createWebHashHistory(),
  routes
})
    /*
    * 路由前置守卫：每次转发路由前执行的函数
* param to: 来源地址
* param from: 目标地址
* next: 放行函数
*/
router.beforeEach((to, from, next) => {
  const vuex = useStore();
  // 登录页面直接放行
  if (to.path === '/') {
    next();
  }
  // 其他页面都需要判断是否已登陆
  else {
    if (!vuex.state['loginFlag']) {
      ElMessage.warning('请先登录！');
      setTimeout(() => next('/'), 2000);
    }
    else {
      next();
    }

  }
});
export default router
